package kalender.backend;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.joda.time.DateTime;
import org.postgresql.util.Base64;


public class TestProgram {
	public static void main(String[] args) throws BackendException {
		testLuser();
		//testEventType();
		//testEvent();
		//testLuserPic();
	}
	
	private static void testLuserPic() throws BackendException {
		LuserPic lp = LuserPic.findByUser(Luser.find(1));
		
		// Write the image to file
		File file = new File("C:/Users/Gustaf/a11.jpg");
		try {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(lp.getPicture());
			fos.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// byte[] bytes = lp.getPicture();
		// int[] ints = new int[bytes.length];
		// for (int i = 0; i < bytes.length; i++) {
			// ints[i] = (int) bytes[i] & 0xFF;
		// }
		
		// String s = Base64url.encode(ints);
		
		// int[] ints2 = new int[bytes.length];
		// ints2 = Base64url.decode(s);
		
		// for (int k = 0; k < bytes.length; k++) {
			// if (ints[k] != ints2[k]) System.out.println("err");
		// }
		
		// byte[] bytes2 = new byte[bytes.length];
		// for (int l = 0; l < bytes.length; l++) {
			// bytes2[l] = (byte) ints2[l];
		// }
		
		// for (int k2 = 0; k2 < bytes.length; k2++) {
			// if (bytes[k2] != bytes2[k2]) System.out.println("err2");
		// }
		
//		// Change the image
//		File file2 = new File("D:/b1.jpg");
//		try {
//			FileInputStream fis = new FileInputStream(file2);
//			byte[] data = new byte[1024*1024*5];
//			fis.read(data);
//			lp.setPicture(data);
//			lp.saveUpdate();
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
//		// New image
//		LuserPic lp2 = new LuserPic();
//		lp2.setUid(Luser.find(2));
//		File file3 = new File("D:/c.jpg");
//		try {
//			FileInputStream fis = new FileInputStream(file3);
//			byte[] data = new byte[1024*1024*5];
//			fis.read(data);
//			lp2.setPicture(data);
//			lp2.saveNew();
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	private static void testEvent() throws BackendException {
		DateTime dt1 = new DateTime(1900, 1, 1, 0, 0, 0, 0);
		DateTime dt2 = new DateTime(2020, 1, 1, 0, 0, 0, 0);
		System.out.println("Defaults events");
		List<Event> events = Event.findByUserAndDate(Luser.find(1), dt1.toDate(), dt2.toDate());
		for (Event e : events) 
			System.out.println(e);
		System.out.println("Public events:");
		events = Event.findPublicByDate(dt1.toDate(), dt2.toDate());
		for (Event e : events) 
			System.out.println(e);
		
		// Create new
		Event e1 = new Event();
		e1.setE_desc("Derp12");
		e1.setEtype(EventType.find("Arbete"));
		e1.setUid(Luser.find("default"));
		e1.setStart_ts(new java.util.Date());
		e1.setStop_ts(new java.util.Date());
		e1.saveNew();
		
		// Change 
		Event e2 = Event.find(1);
		e2.setStart_ts(new java.util.Date());
		e2.saveUpdate();
	}
	
	private static void testEventType() throws BackendException {
		List<EventType> list = EventType.findByUser(Luser.find(1));
		for (EventType et : list) System.out.println(et);
		
		EventType e1 = EventType.find(1);
		System.out.println(e1);
	
		
		// Create new
		EventType e2 = new EventType();
		e2.setEt_desc("Chilla");
		e2.saveNew();
		
		// Change
		e2.setEt_desc("Aids");
		e2.saveUpdate();
		
		// Delete
		EventType.delete(e2.getEtype()); 
	}
	
	public static void testLuser() throws BackendException {
		// Find by username
		Luser l0 = Luser.find("default");
		System.out.println(l0);
        System.out.println(l0.getCg_class());
		
		Luser l1 = Luser.find("danne");
        System.out.println(l1.getCg_class());
        System.out.println(l1.getCg_program());
        
        l1.setCg_class("derping");
        l1.setCg_program("derpprogram1");
        l1.saveUpdate();
		
		// Create new without cg_program
		Luser l2 = new Luser();
		l2.setUname("derp1");
		l2.setPasswd("pw");
		l2.setName("Hurr");
		l2.saveNew();
        
        Luser l3 = new Luser();
        l3.setUname("derp2");
        l3.setPasswd("pw");
        l3.setName("Hurr");
        l3.setCg_class("CustomGreeter22");
        l3.setCg_program("derp derp");
        l3.saveNew();
	}
}
